<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="老师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入老师名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老师手机" prop="teacherMobile">
        <el-input
          v-model="queryParams.teacherMobile"
          placeholder="请输入老师手机"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="在职状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择在职状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_staff_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['school:teacher:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['school:teacher:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['school:teacher:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['school:teacher:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工号" align="center" prop="teacherNumber" />
      <el-table-column label="老师名称" align="center" prop="teacherName" />
      <el-table-column label="老师手机" align="center" prop="teacherMobile" />
      <el-table-column label="老师头像" align="center" prop="headUrl">
        <template slot-scope="scope">
          <el-image 
            v-if="scope.row.headUrl"
            style="width: 40px; height: 40px"
            :src="uploadImgUrl + scope.row.headUrl" 
            :preview-src-list="[uploadImgUrl + scope.row.headUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="老师性别" align="center" prop="teacherSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.teacherSex"/>
        </template>
      </el-table-column>
      <el-table-column label="老师生日" align="center" prop="teacherBirthday" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.teacherBirthday ? parseTime(scope.row.teacherBirthday, '{y}-{m}-{d}') : '- -' }}</span>
        </template>
      </el-table-column>
       <el-table-column label="在职状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_staff_type" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-paddingh" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            :class="scope.row.remake ? '' : 'disable'"
            @click="downLoadFile(scope.row)"
            v-hasPermi="['school:teacher:download']"
          >下载</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['school:teacher:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['school:teacher:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <TeacherModel ref="TeacherModel" @updatedata="updateTeacherData" />
  </div>
</template>

<script>
import { listTeacher, getTeacher, delTeacher, exportTeacher } from "@/api/school/teacher";
import TeacherModel from '../model/teacherModel.vue'
export default {
  name: "Teacher",
  dicts: ['sys_user_sex','sys_staff_type'],
  components: {TeacherModel},
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老师信息表格数据
      teacherList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        teacherMobile: null,
      }
    };
  },
  created() {
    this.getList();
    
  },
  methods: {
    downLoadFile(row) {
      if (!row.remake) return
      window.location.href = this.uploadImgUrl + row.remake
    },
    updateTeacherData() {
      this.getList()
    },
    /** 查询老师信息列表 */
    getList() {
      this.loading = true;
      listTeacher(this.queryParams).then(response => {
        this.teacherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.TeacherModel.reset();
      this.$refs.TeacherModel.open = true;
      this.$refs.TeacherModel.title = "添加老师信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.TeacherModel.reset();
      const id = row.id || this.ids
      getTeacher(id).then(response => {
        this.$refs.TeacherModel.form = response.data;
        this.$refs.TeacherModel.form.fileList = response.data.remake ? response.data.remake.split(',').map(item => { return {name: item, url: ''} }) : []
        this.$refs.TeacherModel.open = true;
        this.$refs.TeacherModel.title = "修改老师信息";
      });
    },
    
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除所选老师信息？').then(function() {
        return delTeacher(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有老师信息数据项？').then(() => {
        this.exportLoading = true;
        return exportTeacher(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
<style scoped lang="scss">
  .disable {
    color: #999;
  }
</style>
